// Package transport
// Author liuzhiyong
// Date 2023/11/28
// Description
package transport

import (
	"context"
	innerendpoint "gitee.com/liu_zy_developer/go-kit-demo/endpoint"
	"gitee.com/liu_zy_developer/go-kit-demo/model/rpc_model"
	"gitee.com/liu_zy_developer/go-kit-demo/proto"
	"github.com/go-kit/kit/endpoint"
	grpctransport "github.com/go-kit/kit/transport/grpc"
	"google.golang.org/grpc"
)

// --------------------远程调用转换函数---------------------------

// gRPC

// 请求参数
// encodeTrimRequest 将内部使用的数据编码为proto
func encodeTrimRequest(_ context.Context, response interface{}) (request interface{}, err error) {
	resp := response.(rpc_model.TrimRequest)
	return &proto.TrimRequest{S: resp.S}, nil
}

// decodeTrimResponse 解析pb消息
func decodeTrimResponse(_ context.Context, in interface{}) (interface{}, error) {
	resp := in.(*proto.TrimResponse)
	return rpc_model.TrimResponse{S: resp.S}, nil
}

// NewTrimGrpcClient 创建客户端, 返回端点
func NewTrimGrpcClient(conn *grpc.ClientConn) endpoint.Endpoint {
	client := grpctransport.NewClient(
		conn,
		"proto.Trim", // 服务名
		"TrimSpace",  // 方法名
		encodeTrimRequest,
		decodeTrimResponse,
		proto.TrimResponse{},
	)
	return innerendpoint.MakeTrimEndpoint(client)
}
